<template>
  <music-list :title='rank.topTitle'
              :avatar='picUrl'
              :songs='songList'
              :rank='true'></music-list>
</template>

<script>
import MusicList from 'components/music-list/music-list'
import { mapState } from 'vuex'
import { getRankSongList } from 'api/rank'
import { ERR_OK } from 'api/config'
import { awaitWrap } from 'common/js/dom'
import { createSong } from 'common/js/song'
export default {
  data () {
    return {
      songList: [],
      pic_album: ''
    }
  },
  methods: {
    async _getSongList () {
      if (!this.rank.id) return this.$router.back()
      const [err, data] = await awaitWrap(getRankSongList(this.rank.id))
      if (data.code === ERR_OK) {
        // this.rankList = data.data.topList
        this.pic_album = data.topinfo.pic_album
        this.songList = await this._normalSong(data.songlist)
      }
      if (err) { }
    },
    async _normalSong (res) {
      let ret = []
      for (let i = 0; i < res.length; i++) {
        let obj = await createSong(res[i].data, i)
        ret.push(obj)
      }
      return ret
    }
  },
  created () {
    this._getSongList()
  },
  computed: {
    ...mapState([
      'rank'
    ]),
    picUrl () {
      return this.pic_album
    }
  },
  components: {
    MusicList
  }
}
</script>

<style scoped>
</style>